﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Extension {
  public class CommonEqualityComparer<T, V>: IEqualityComparer<T> {
    private readonly Func<T, V> _keySelector;
    private readonly IEqualityComparer<V> _comparer;

    public CommonEqualityComparer(Func<T, V> keySelector, IEqualityComparer<V> comparer) {
      _keySelector = keySelector;
      _comparer = comparer;
    }

    public CommonEqualityComparer(Func<T, V> keySelector) : this(keySelector, EqualityComparer<V>.Default) { }

    public bool Equals(T x, T y) => _comparer.Equals(_keySelector(x), _keySelector(y));

    public int GetHashCode(T obj) => _comparer.GetHashCode(_keySelector(obj));
  }
}
